package com.nf.health.app.customview;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.utils.ViewUtil;

/**
 * Created by Jason on 2016/3/18.
 */
public class MenuPopupWindow extends PopupWindow implements View.OnClickListener {

    private View view;
    TextView tvDeviceName, tvBindState, tvManualInput, tv_loadl_input,tv_sweat_set;
    View ll_bind_device;

    public MenuPopupWindow(Activity act, String deviceName, boolean isManual) {
        super(act);

        view = LayoutInflater.from(act).inflate(R.layout.pop_more_menu, null);

        this.setContentView(view);
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setAnimationStyle(android.R.style.Animation_Dialog);
        this.setBackgroundDrawable(new BitmapDrawable());//必要,保证背景是自定义的.9patch图

        ll_bind_device = ViewUtil.getView(view, R.id.ll_bind_device);
        tv_sweat_set = ViewUtil.getView(view, R.id.tv_sweat_set);
        tvDeviceName = ViewUtil.getView(view, R.id.tv_device_name);
        tvBindState = ViewUtil.getView(view, R.id.tv_bind_state);
        tvBindState = ViewUtil.getView(view, R.id.tv_bind_state);
        tv_loadl_input = ViewUtil.getView(view, R.id.tv_loadl_input);
        tvManualInput = ViewUtil.getView(view, R.id.tv_manual_input);
        ll_bind_device.setOnClickListener(this);
        tvManualInput.setOnClickListener(this);
        tv_loadl_input.setOnClickListener(this);
        tv_sweat_set.setOnClickListener(this);

        if (!isManual) {
            ViewUtil.getView(view, R.id.line).setVisibility(View.GONE);
            tvManualInput.setVisibility(View.GONE);
        }


        if (!TextUtils.isEmpty(deviceName)) {
            tvDeviceName.setText(deviceName);
            tvDeviceName.setVisibility(View.VISIBLE);
            tvBindState.setText("解除绑定");
        } else {
            tvDeviceName.setVisibility(View.GONE);
            tvBindState.setText("绑定设备");
        }
    }

    public MenuPopupWindow(Activity act, String deviceName) {
        this(act, deviceName, true);
    }

    private OnMyClickListener listener;

    public void setOnMenuItemClickListener(OnMyClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        this.dismiss();
        if (listener != null) {
            if (v.getId() == R.id.ll_bind_device) {
                listener.onClick(R.id.ll_bind_device, "");
            } else if (v.getId() == R.id.tv_manual_input) {
                listener.onClick(R.id.tv_manual_input, "");
            } else if (v.getId() == R.id.tv_loadl_input) {
                listener.onClick(R.id.tv_loadl_input, "");
            } else if (v.getId() == R.id.tv_sweat_set) {
                listener.onClick(R.id.tv_sweat_set, "");
            }
        }
    }

    public void setMyHandInputVisibile(int visibile) {
        tvManualInput.setVisibility(visibile);
    }

    public void setSweatSetVisible(int visibile) {
        tv_sweat_set.setVisibility(visibile);
    }

    public void setMyHandInputText(String text) {
        tvManualInput.setText(text);
    }

}
